import { Component } from '@angular/core';

import { NavParams, ViewController } from 'ionic-angular';

import { TaskDetailService } from './detail.service';

import { Config } from '../../../providers/config';

import _ from 'lodash';


@Component({
  selector: 'page-task-flowLog',
  templateUrl: './flowLog.html',
  providers: [TaskDetailService]
})
export class FlowLogPage {
	moa_page_name: any = "page-task-flowLog";
  moa_page_explain: any = "流程 - 明细信息";

  // 流程实例ID
  flowProcessId;
  // 格式化后的明细列表数据
  formatedItem: any = [];

  constructor(
    public navParams: NavParams,
    public viewCtrl: ViewController,
    public detailService: TaskDetailService
  ) {
    this.flowProcessId = this.navParams.get('flowProcessId');

    this.dataFomate();

  }

  complete() {
    this.dismiss();
  }

  dismiss(data?: any) {
    // using the injected ViewController this page
    // can "dismiss" itself and pass back data
    this.viewCtrl.dismiss(data);
  }

  checkIsSelected(item) {
    item.isShow = !item.isShow;
  }

  /**
   * 格式化数据
   * [
   * {
   *  "name" :;
   *  "date" :;
   *  "isShow":;
   *  "data" :
   * },{
   *  "name" :;
   *  "date" :;
   *  "isShow":;
   *  "data" :
   * }
   * ]
   */
  dataFomate() {
    let that = this;
    this.detailService.getFlowLog({ "loginId": "", "flowProcessId": this.flowProcessId }).subscribe(logList => {
      if (logList) {
        _.each(logList, function(data, index) {
          let temp: any = {};
          temp.name = data.currNodeName;
          temp.date = data.createTime;
          temp.isShow = false;
          temp.data = data;
          that.formatedItem.push(temp);
        })

        that.formatedItem = _.orderBy(that.formatedItem,["date"],["asc"]); // 按时间顺序排序
      }
    });
  }
}
